package com.fastloan.jn.mgt.customermanager.task.controller;

import com.fastloan.jn.mgt.customermanager.basic.model.JnCustomerManager;
import com.fastloan.jn.mgt.customermanager.daily.service.JnCustomerManagerDailyService;
import com.fastloan.jn.mgt.customermanager.task.model.JnCustomerManagerTask;
import com.fastloan.jn.mgt.customermanager.task.model.JnManagerTaskEntry;
import com.fastloan.jn.mgt.customermanager.task.service.JnCustomerManagerTaskService;
import com.fastloan.jn.mgt.customermanager.task.service.JnManagerTaskEntryService;
import com.fastloan.jn.utils.DateTimeUtil;
import com.fastloan.jn.utils.other.ObjUtil;
import com.fastloan.safe.core.util.ManagerKit;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.HtmlUtils;

import java.lang.reflect.Type;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by gelon on 2017/10/29.
 */
@RestController
@RequestMapping("/api/web/managerTask")
public class ManagerTaskController extends BaseController<JnCustomerManagerTask>{

    @Autowired
    JnCustomerManagerTaskService taskService ;

    @Autowired
    JnCustomerManagerDailyService dailyService;

    @Autowired
    JnManagerTaskEntryService taskEntryService;

    @PostMapping("/customers")
    public ResultTo getManagers(){
        List<String> ids = dailyService.topOrgManagerIds(ManagerKit.getTopOrgId());
        Map<String,Object> map = new HashMap<>();
        map.put("ids",ids);
        map.put("date", DateTimeUtil.format(new Date(),DateTimeUtil.NORM_DATE_PATTERN));
        return dataPage("userTasks",map);
    }

    @GetMapping("/task")
    public ResultTo getTask(String taskId,Date date) throws ParseException {
        if(date == null) { date = new Date(); }
        String date_str = DateTimeUtil.format(date,DateTimeUtil.NORM_DATE_PATTERN);
        Map<String,Object> map = new HashMap<>();
        map.put("taskId",taskId);
        map.put("date",date_str);
        JnCustomerManagerTask customerManagerTask = taskService.selectInfo(map) ;
        customerManagerTask.setManagerTaskEntries(taskEntryService.select(new JnManagerTaskEntry().setTaskId(customerManagerTask.getId())));
        return new ResultTo().setData(customerManagerTask);
    }

    @PostMapping("/saveUpdate")
    public ResultTo saveUpdate(JnCustomerManagerTask managerTask, String customerManagerTaskJson){
        customerManagerTaskJson = HtmlUtils.htmlUnescape(customerManagerTaskJson);
        Gson gson = new Gson();
        Type type = new TypeToken<List<JnManagerTaskEntry>>(){}.getType();

        List<JnManagerTaskEntry> jnManagerTaskEntries = gson.fromJson(customerManagerTaskJson,type);
        managerTask.setManagerTaskEntries(jnManagerTaskEntries);
        taskService.saveUpdate(managerTask,jnManagerTaskEntries);
        return new ResultTo();
    }








}
